/*
 * Copyright (c) 2015 LingoChamp Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.living.player.ui.recently.audio;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.living.R;
import com.living.filedownloader.DownLoadManger;
import com.living.filedownloader.ui.TasksDownloadingActivity;
import com.living.player.db.RecentlyManger;
import com.living.widget.BottomMenuView;
import com.md.livingwidget.CoreBaseActivity;
import com.md.livingwidget.toast.PHToast;
import com.md.network.api.Audio;

import java.util.ArrayList;
import java.util.Map;

import androidx.fragment.app.FragmentManager;

/**
 * 已下载 批量的操作
 * 操作
 */
public class RecentlyAudioBatchActivity extends CoreBaseActivity {

    private BottomMenuView bottom_menu;
    private RecentlyAudioBatchFragment audioBatchFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recently_audio_batch_delete);
        FragmentManager fragmentManager = getSupportFragmentManager();
        audioBatchFragment = (RecentlyAudioBatchFragment) fragmentManager.findFragmentById(R.id.fm_batch_delete);

        bottom_menu = findViewById(R.id.bottom_menu);
        bottom_menu.setMenuShows(true, true, false, true);

        bottom_menu.setMenuAddEnable(false);
        bottom_menu.setMenuDownloadEnable(false);
        bottom_menu.setMenuShareEnable(false);
        bottom_menu.setMenuDeleteEnable(false);

        audioBatchFragment.setOnBatchSelectLisenter(new RecentlyAudioBatchFragment.OnBatchSelectLisenter() {
            @Override
            public void onSelect(int count, int total) {
                if (count > 0) {
                    bottom_menu.setMenuAddEnable(true);
                    bottom_menu.setMenuDownloadEnable(true);
                    bottom_menu.setMenuShareEnable(true);
                    bottom_menu.setMenuDeleteEnable(true);
                } else {
                    bottom_menu.setMenuAddEnable(false);
                    bottom_menu.setMenuDownloadEnable(false);
                    bottom_menu.setMenuShareEnable(false);
                    bottom_menu.setMenuDeleteEnable(false);
                }
            }
        });
        bottom_menu.setOnBottomMenuViewClick(new BottomMenuView.OnBottomMenuViewClick() {
            @Override
            public void onClickAdd(View view) {
//                PHToast.showSingleToast(getmActivity(), "去李越专辑添加页面");
                Map<String, Audio> mapSel = audioBatchFragment.getMapSel();
                ArrayList<Audio> audioArrayList = new ArrayList<>();
                audioArrayList.addAll(mapSel.values());
                ARouter.getInstance()
                        .build("/album/albumlist")
                        .withParcelableArrayList("SELECTED_AUDIOS", audioArrayList)
                        .navigation();
            }

            @Override
            public void onClickDownload(View view) {
                Map<String, Audio> mapSel = audioBatchFragment.getMapSel();
                if (mapSel.size() > 0) {
                    ArrayList<Audio> audioArrayList = new ArrayList<>();
                    //遍历map中的值
                    for (Audio audio : mapSel.values()) {
                        audioArrayList.add(audio);
                    }
                    DownLoadManger.getInstance().downloadAudioList(audioArrayList);
                    PHToast.shotBigToast(getmContext(), "添加下载成功");
                    finish();
                } else {
                    PHToast.showSingleToast(getmContext(), "请选择要下载的");
                }
            }

            @Override
            public void onClickShare(View view) {
                PHToast.showSingleToast(getmActivity(), "分享组件");
            }

            @Override
            public void onClickDelete(View view) {
                Map<String, Audio> mapSel = audioBatchFragment.getMapSel();
                if (mapSel.size() > 0) {
                    setResult(Activity.RESULT_OK);
                    //遍历map中的值
                    for (Audio value : mapSel.values()) {
                        RecentlyManger.getInstance().deleteAudio(value.getId());
                        mapSel.remove(value);
                        audioBatchFragment.remove(value);
                    }
                } else {
                    PHToast.showSingleToast(getmActivity(), "请选择要删除的");
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.layout_downloading) {
            startActivity(new Intent(this, TasksDownloadingActivity.class));
        } else if (v.getId() == R.id.iv_downloaded_edite) {

        }
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}
